package com.zadetk.helpyou.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadetk.helpyou.R;
import com.zadetk.helpyou.adapter.MyAdapter;
import com.zadetk.helpyou.base.AbsActivity;
import com.zadetk.helpyou.bean.SearchItemBean;
import com.zadetk.helpyou.view.NoScrollView;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 4-3-1搜索页面
 */
public class SearchItemActivity extends AbsActivity {
    private EditText etSearch;
    private TextView tvCancel;
    private NoScrollView gvTag;
    private LinearLayout llHotTag;
    private TagFlowLayout flTag;
    private LinearLayout llList;
    private RecyclerView recycleview;
    private ArrayList<String> list = new ArrayList<>();
    private List<SearchItemBean> data = new LinkedList<>();
    private SearchAdapter searchAdapter;
    private List<String> tagList = new LinkedList<>();

    /**
     * Find the Views in the layout<br />
     * <br />
     * Auto-created on 2018-05-14 13:40:54 by Android Layout Finder
     * (http://www.buzzingandroid.com/tools/android-layout-finder)
     */
    private void initViews() {
        etSearch = (EditText) findViewById(R.id.et_search);
        tvCancel = (TextView) findViewById(R.id.tv_cancel);
        gvTag = (NoScrollView) findViewById(R.id.gv_tag);
        llHotTag = (LinearLayout) findViewById(R.id.ll_hot_tag);
        flTag = (TagFlowLayout) findViewById(R.id.fl_tag);
        llList = (LinearLayout) findViewById(R.id.ll_list);
        recycleview = (RecyclerView) findViewById(R.id.recycleview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleview.setLayoutManager(linearLayoutManager);
        setTopTitle("发布服务");
        llList.setVisibility(View.GONE);
        llHotTag.setVisibility(View.GONE);
    }

    @Override
    public int setView() {
        return R.layout.activity_search_item;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initViews();
        initLogic();
    }

    private void initLogic() {
        list.add("");
        list.add("");
        list.add("");
        list.add("");
        list.add("");
        list.add("");
        list.add("");
        gvTag.setAdapter(new MyAdapter<String>(list, R.layout.search_item) {
            @Override
            public void bindView(ViewHolder holder, String obj) {
                holder.getItemView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(SearchItemActivity.this, PublishServiceSubActivity.class));
                    }
                });
            }
        });
        etSearch.clearFocus();
        etSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    llHotTag.setVisibility(View.VISIBLE);
                    llList.setVisibility(View.GONE);
                    gvTag.setVisibility(View.GONE);
                    tvCancel.setVisibility(View.VISIBLE);
                } else {
                    tvCancel.setVisibility(View.GONE);
                    llHotTag.setVisibility(View.GONE);
                    llList.setVisibility(View.GONE);
                    gvTag.setVisibility(View.VISIBLE);
                }
            }
        });
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(editable.toString())) {
                    if (etSearch.hasFocus()) {
                        llHotTag.setVisibility(View.VISIBLE);
                        llList.setVisibility(View.GONE);
                        gvTag.setVisibility(View.GONE);
                    } else {
                        llHotTag.setVisibility(View.GONE);
                        llList.setVisibility(View.GONE);
                        gvTag.setVisibility(View.VISIBLE);
                    }
                } else {
                    llHotTag.setVisibility(View.GONE);
                    llList.setVisibility(View.VISIBLE);
                    gvTag.setVisibility(View.GONE);
                }
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvCancel.setVisibility(View.GONE);
                etSearch.clearFocus();
            }
        });
        data.add(new SearchItemBean());
        data.add(new SearchItemBean());
        data.add(new SearchItemBean());
        data.add(new SearchItemBean());
        searchAdapter = new SearchAdapter(R.layout.search_item2, data);
        searchAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(SearchItemActivity.this, PublishServiceActivity.class));
            }
        });
        recycleview.setAdapter(searchAdapter);
        tagList.add("品牌电脑维修");
        tagList.add("iphone手机");
        tagList.add("搬家");
        tagList.add("全城送取");
        tagList.add("墙面刷新");
        tagList.add("代办证件");

        final TagAdapter tagAdapter = new TagAdapter<String>(tagList) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) getLayoutInflater().inflate(R.layout.tag_text, null);
                tv.setText(s);
                return tv;
            }
        };
        flTag.setAdapter(tagAdapter);
        flTag.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                startActivity(new Intent(SearchItemActivity.this, PublishServiceActivity.class));
                return false;
            }
        });
    }

    private class SearchAdapter extends BaseQuickAdapter<SearchItemBean, BaseViewHolder> {

        public SearchAdapter(int layoutResId, @Nullable List<SearchItemBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, SearchItemBean item) {

        }
    }
}
